﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using Jb.Web.Utility;
using AutoMapper;
using Jb.Web.ViewModels.UtilityModels;
using Jb.Search;

namespace Jb.Web.Areas.Private.Controllers
{
    [Authorize(Roles = "Company Admin,Company User,Admin")]
    public class ResumeController : BaseController
    {
		private JbEntities _Context = new JbEntities();
        
		#region Constructors
		public ResumeController()
		{
		}

		public ResumeController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index(int pageNo = 0)
        {            
            return View("Favorites", BuildResumeListConfig(pageNo <= 0 ? 1 : pageNo));
        }

        /// <summary>
        /// Lists favorite Resume
        /// </summary>
        /// <param name="pageNo"></param>
        /// <returns></returns>
		public ActionResult Favorites(int pageNo = 0)
        {            
            return View(BuildResumeListConfig(pageNo <= 0 ? 1 : pageNo));
        }


        public ActionResult Search(String key, String pageNo)
        {
            if (key.IsNullOrEmpty())
                return View(new ListConfig(null, new PagerConfig()));
            key = key.UrlDecode();
            PagerConfig pagerConfig = new PagerConfig(pageNo.ToInt() > 0 ? pageNo.ToInt() : 1, ConfigReader.PageSize, 0);

            int totalRecord = 0;
            ///Search in Lucene Index
            Jb.Search.ISearcher resumeSearcher = SearcherFactory.GetSearcher(typeof(Resume));

            String resumeIDs = resumeSearcher.Search(key, pagerConfig.PageSize, pagerConfig.PageNo, out totalRecord); //"22,29";

            pagerConfig.TotalRecord = totalRecord;

            ///Get the Resulting Product Information from Database
            UtilityDAO dao = new UtilityDAO();
            DbParameter[] parameters = new[] { 
                new DbParameter("@ResumeIDs", resumeIDs)
                //, new DbParameter("@UserID", SessionCache.CurrentUser == null ? 0 : SessionCache.CurrentUser.ID)
            };
            DataSet ds = dao.GetPagedData(DbObject.SP.GET_PAGED_RESUMES_BY_ID, parameters, pagerConfig.PageNo, pagerConfig.PageSize, out totalRecord, true);
            ListConfig listConfig = new ListConfig(ds.Tables[0], pagerConfig);
            pagerConfig.TotalRecord = totalRecord;
            ViewData[AppConstants.ViewDataKeys.SEARCH_KEY] = key;
            return View(listConfig);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Search(String keyword)
        {
            keyword = keyword.UrlEncode();
            return RedirectToAction("search", new { key = keyword, pageNo = 1 });
        }

		
		#endregion Action Methods
		
		#region Helper Methods

		/// <summary>
        /// Builds a ListConfig object with a list of Resume
        /// </summary>
        /// <returns></returns>
		private ListConfig BuildResumeListConfig(int pageNo)
		{
            ///TODO: Not Implemented fully
			UtilityDAO dao = new UtilityDAO();
            int totalRecord = 0;
            PagerConfig pager = new PagerConfig(pageNo, ConfigReader.PageSize, totalRecord);            
            Jb.Domain.DbParameter[] parameters = new[]{
                new Jb.Domain.DbParameter("@CompanyID", LoggedInUser.Contact.CompanyID)
            };
            DataSet ds = dao.GetPagedData(DbObject.SP.GET_PAGED_FAVOURITE_RESUME_LIST, parameters, pager.PageNo, pager.PageSize, out totalRecord, true);
            pager.TotalRecord = totalRecord;
            ListConfig listConfig = new ListConfig(ds.Tables[0], pager);
            return listConfig;
		}

        #endregion			
    }
}


